package silo.dungeonbuddy;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Vector;

public class MapForm extends Form
{
	private int squaresPerScreen;
	private IntValue offsetX = new IntValue(0);
	private IntValue offsetY = new IntValue(0);
	private Board gameBoard;

	public MapForm(String eName, Start eGame, int eX, int eY, int eWidth, int eHeight, String eTitle)
	{
		super(eName,eGame,eX,eY,eWidth,eHeight,eTitle);
		
		//TODO: fix this - I don't need nearly this much stuff
		
		//INIT VARIABLES
		squaresPerScreen=20;
		width=800;
		height=600;
		offsetX.setValue(0);
		offsetY.setValue(0);
		
		// GET CONTEXT FOR ACCELERATED GRAPHICS
		gameBoard = game.getBoard();
		int hdiv = height/squaresPerScreen;
		scrollBars.add(new ScrollBar(width-20,0,20,height,true,offsetY,hdiv*gameBoard.getHeight()));
		scrollBars.add(new ScrollBar(0,height-20,20,width,false,offsetX,hdiv*gameBoard.getWidth()));
	}//MapForm()
	
	public void draw()
	{
		// BLANK THE WINDOW TO BACK COLOR (This was important at 
		// one point, but not really anymore.  I was going to have an
		// option to change background color, but now I just use a
		// background image, instead.
		Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
		g.setColor(Color.darkGray);
		g.fillRect(0,0,width,height);
		
		//DRAW BACKGROUND
		gameBoard.getBackground().draw(g,this);
		
		//DRAW GRID
		g.setColor(Color.gray);
		int rows = gameBoard.getHeight();
		int cols = gameBoard.getWidth();
		int hdiv = height/squaresPerScreen;
		int hdtro = hdiv*(rows-1);
		int hdtco = hdiv*(cols-1);
		for(int i=(int)Math.floor((0-offsetX.getValue())/hdiv);((i<cols)&&(i<((int)Math.floor((0-offsetX.getValue())/hdiv)+squaresPerScreen+10)));i++)
		{
			g.drawLine(0+offsetX.getValue(), 0+offsetY.getValue(), 0+offsetX.getValue(), hdtro+hdiv+offsetY.getValue());
			g.drawLine((hdiv*i)+hdiv+offsetX.getValue(),offsetY.getValue(),(hdiv*i)+hdiv+offsetX.getValue(),hdtro+hdiv+offsetY.getValue());
		}
		for(int i=(int)Math.floor((0-offsetY.getValue())/hdiv);((i<rows)&&(i<((int)Math.floor((0-offsetY.getValue())/hdiv)+squaresPerScreen+1)));i++)
		{
			g.drawLine(offsetX.getValue(), offsetY.getValue(), hdtco+hdiv+offsetX.getValue(), offsetY.getValue());
			g.drawLine(offsetX.getValue(),(hdiv*i)+hdiv+offsetY.getValue(),hdtco+hdiv+offsetX.getValue(),(hdiv*i)+hdiv+offsetY.getValue());
		}
		
		//DRAW SQUARES
		if(gameBoard.getSquareList().size()>0)
		{
			for(int i=0; i<gameBoard.getSquareList().size();i++)
			{
				Vector v = (Vector)gameBoard.getSquareList().get(i);
				if(v.size()>0)
				{
					for(int j=0; j<v.size();j++)
					{
						Square s = (Square)v.get(j);
						s.draw(g,this);
					}//next j
				}
			}//next i
		}
		
		//DRAW ALL SPRITES IN THE terrainList Vector
		if(!game.getTerrainList().isEmpty())
		{
			for (int i=0;i<game.getTerrainList().size();i++)
			{
				FlatImage item = (FlatImage) game.getTerrainList().get(i);
				if((item.getVisible())&&(!game.getClient()))
				{
					item.draw(g,this);
				}else
				{
					if((item.getVisible())&&(game.getClient()))
					{
						item.draw(g,this);
					}
				}
			}//next i
		}
		
		//DRAW ALL SPRITES IN THE unitList Vector
		if(!game.getUnitList().isEmpty())
		{
			for (int i=0;i<game.getUnitList().size();i++)
			{
				FlatImage item = (FlatImage) game.getUnitList().get(i);
				if((item.getVisible())&&(!game.getClient()))
				{
					item.draw(g,this);
				}else
				{
					try
					{
						Unit u = (Unit)item;
						if((item.getVisible())&&(game.getClient())&&(game.determineCover(game.getMyUnit(),u)<16)&&((game.getMyUnit().getPerception()>=u.getStealth())||((Math.abs(game.getMyUnit().getColumn()-u.getColumn())<2)&&(Math.abs(game.getMyUnit().getRow()-u.getRow())<2))||(game.determineCover(game.getMyUnit(),u)==0)))
						{
							item.draw(g,this);
						}
					}catch(Exception e)
					{
						if(game.getClient())
						{
							item.draw(g,this);
						}
					}//catch
				}
			}//next i
		}
		//DRAW ALL SPRITES IN THE markerList Vector
		if(!game.getMarkerList().isEmpty())
		{
			for (int i=0;i<game.getMarkerList().size();i++)
			{
				FlatImage item = (FlatImage) game.getMarkerList().get(i);
				if((item.getVisible())&&(!game.getClient()))
				{
					item.draw(g,this);
				}else
				{
					if((item.getVisible())&&(game.getClient()))
					{
						item.draw(g,this);
					}
				}
			}//next i
		}
		
		//DRAW SCROLLBARS
		for(int i=0;i<scrollBars.size();i++)
		{
			ScrollBar s = (ScrollBar)scrollBars.get(i);
			s.draw(g);
		}//next i
		
		//DISPOSE OF GRAPHICS OBJECT AND FLIP BUFFER
		g.dispose();
		try
		{
			strategy.show();
		}catch(Exception e)
		{
			createBufferStrategy(2);
			strategy = getBufferStrategy();
			strategy.show();
		}
	}//draw()
	
	public void checkKeys()
	{
		// I WOULD LIKE TO MOVE THIS SUBROUTINE TO A DIFFERENT
		// CLASS FOR ORGANIZATION PURPOSES, BUT I HAVEN'T DONE
		// IT YET...
		if (kLeft)
		{
			//player.moveLeft();
			if((kShift)&&(offsetX.getValue()<5))
			{
				offsetX.setValue(offsetX.getValue()+5);
			}else if(offsetX.getValue()<10)
			{
				offsetX.setValue(offsetX.getValue()+1);
			}
		}
		if (kRight)
		{
			//player.moveRight();
			//if(offsetX>-10)
			//{
			if(kShift)
			{
				offsetX.setValue(offsetX.getValue()-5);
			}else
			{
				offsetX.setValue(offsetX.getValue()-1);
			}
			//}
		}
		if (kUp)
		{
			//player.moveUp();
			if((kShift)&&(offsetY.getValue()<5))
			{
				offsetY.setValue(offsetY.getValue()+5);
			}else if(kControl)
			{
				squaresPerScreen=squaresPerScreen-1;
				kUp=false;
			}else if(offsetY.getValue()<10)
			{
				offsetY.setValue(offsetY.getValue()+1);
			}
		}
		if (kDown)
		{
			//player.moveDown();
			if(kShift)
			{
				offsetY.setValue(offsetY.getValue()-5);
			}else if(kControl)
			{
				squaresPerScreen=squaresPerScreen+1;
				kDown=false;
			}else
			{
				offsetY.setValue(offsetY.getValue()-1);
			}
		}
	}//checkKeys
	
	//GETTERS
	public int getSquaresPerScreen()
	{
		return squaresPerScreen;
	}
	public IntValue getOffsetX()
	{
		return offsetX;
	}
	public IntValue getOffsetY()
	{
		return offsetY;
	}
}//class MapForm